CC=gcc
CFLAGS=-c -Wall -fPIC
LDFLAGS=
#SOURCES=queue.c priority_queue.c logger.c logger_manager.c io_handler.c server_worker.c socket_utils.c user_util.c jobs_controller.c common_objects_pool.c timer_service.c conf/conf_file.c user_pools.c
SOURCES=queue.c priority_queue.c logger.c io_handler.c server_worker.c socket_utils.c user_util.c jobs_controller.c common_objects_pool.c timer_service.c conf/conf_file.c user_pools.c
NET_SOURCES=net/aio/socket.c
OBJECTS=$(SOURCES:.c=.o)
NET_OBJECTS=$(NET_SOURCES:.c=.o)

# fuente y objetos de la aplicacion.
HTTP_SOURCES=http/http_parser.c http/http.c http/accounts_service.c http/http_servlet.c http/server_worker_implementations.c
HTTP_OBJECTS=$(HTTP_SOURCES:.c=.o)

LOGGER_OBJECTS=logger.o

#MAIN_OBJECTS=jobs_controller.o queue.o worker.o server_worker.o io_handler.o $(NET_OBJECTS) $(LOGGER_OBJECTS) $(UTIL_OBJECTS)

UTIL_SOURCES=utils/heap.c utils/hashmap.c utils/object_pool.c utils/bstrlib.c utils/rbtree.c utils/timer_utils.c
UTIL_OBJECTS=$(UTIL_SOURCES:.c=.o)

EXECUTABLE=mashup-server

MASHUP_SERVER_LIB=libmashup-server.so

APPLICATION_LIB=libmashup-app.so

all: $(UTIL_SOURCES) $(NET_SOURCES) $(HTTP_SOURCES) $(SOURCES) $(TEST_SOURCES) $(MASHUP_SERVER_LIB) $(APPLICATION_LIB) $(EXECUTABLE)

#mashup-server: worker.o $(NET_OBJECTS) $(OBJECTS) $(HTTP_OBJECTS) $(UTIL_OBJECTS) mashup-server.c
#	$(CC) $(LDFLAGS) -D_LIBC_REENTRANT -DHTTP_PARSER_STRICT=0 -pthread $(NET_OBJECTS) $(OBJECTS) $(HTTP_OBJECTS) $(UTIL_OBJECTS) worker.o mashup-server.c -o $@ -L/home/ignacio/tesis/mashup-server-2.0 -lrt -ldl

libmashup-server.so: worker.o $(NET_OBJECTS) $(OBJECTS) $(UTIL_OBJECTS)
	$(CC) -shared -Wl,-soname,$@ -o $@ $(NET_OBJECTS) $(OBJECTS) $(UTIL_OBJECTS) worker.o 

libmashup-app.so: $(APPL_IMPLEMENTATION) $(HTTP_OBJECTS)
	$(CC) -shared -Wl,-soname,$@ -o $@ $(APPL_IMPLEMENTATION) $(HTTP_OBJECTS)

mashup-server: $(MASHUP_SERVER_LIB) mashup-server.c
	$(CC)  $(LDFLAGS) -D_LIBC_REENTRANT -DHTTP_PARSER_STRICT=0 -pthread mashup-server.c -o $@ -L. -lmashup-server -lrt -ldl -lconfig -lzlog

#armado de libreria dinamica
#gcc -Wall -fPIC -c server_worker_implementations.c 
#gcc -shared -Wl,-soname,libapp.so.1 -o libapp.so.1.0   server_worker_implementations.o y todos los .o necesarios.
#mv libapp.so.1.0 libapp.so

#server_worker_implementations.o: $(APPL_IMPLEMENTATION) $(HTTP_OBJECTS) $(UTIL_OBJECTS) $(NET_OBJECTS)
#	$(CC) $(CFLAGS) $< -o $@
#	rm -f libswi.a
#	ar -cvq libswi.a $@

worker.o: worker.c
	$(CC) $(CFLAGS) -pthread $< -o $@

.c.o:
	$(CC) $(CFLAGS) $< -o $@
	
http/http_servlet.o: http/http_servlet.c http/http_parser.o http/http.o
	$(CC) $(CFLAGS) $< -o $@
	
clean:
	rm -f $(EXECUTABLE) *.o *.a $(NET_OBJECTS) $(HTTP_OBJECTS) $(UTIL_OBJECTS) $(MASHUP_SERVER_LIB) $(APPLICATION_LIB)

